//
//  KJTableViewManager.m
//  KJKit
//
//  Created by Tiger on 2022/6/9.
//

#import "KJTableViewManager.h"

@implementation KJTableViewManager

- (instancetype)initWithTableView:(UITableView *)tableView {
    self = [super init];
    if (self) {
        self.tableView = tableView;
        tableView.delegate = self;
        tableView.dataSource = self;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
    }
    return self;
}

- (void)setTableHeaderView:(UIView *)tableHeaderView {
    _tableHeaderView = tableHeaderView;
    if (self.tableView) {
        self.tableView.tableHeaderView = _tableHeaderView;
    }
}

- (void)setTableFooterView:(UIView *)tableFooterView {
    _tableFooterView = tableFooterView;
    if (self.tableView) {
        self.tableView.tableFooterView = _tableFooterView;
    }
}

#pragma mark -- tableView Data Source --

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sections[section].items.count;
}

- (KJTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KJTableViewItem *item = self.sections[indexPath.section].items[indexPath.row];
    KJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[item cellID]];
    
    if (!cell) {
        cell = [[[item cellClass] alloc] initWithStyle:item.cellStyle reuseIdentifier:[item cellID]];
    }
    
    item.indexPath = indexPath;
    cell.item = item;
    
    [cell cellWillAppear];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    NSLog(@"%@",self.sections[section].footerTitle);
    return self.sections[section].footerTitle;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSLog(@"%@",self.sections[section].headerTitle);
    return self.sections[section].headerTitle;
}

#pragma mark -- tableView Delegate --

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.sections[indexPath.section].items[indexPath.row].allowSelection;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return self.sections[indexPath.section].items[indexPath.row].cellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    CGFloat height = self.sections[section].headerHeight == 0 ? CGFLOAT_MIN : self.sections[section].headerHeight;
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    CGFloat height = self.sections[section].footerHeight == 0 ? CGFLOAT_MIN : self.sections[section].footerHeight;
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(self.sections[indexPath.section].items[indexPath.item].allowSelection) {
    KJTableViewItem *item = self.sections[indexPath.section].items[indexPath.row];
        item.indexPath = indexPath;
        if (item.selectedAction) {
            item.selectedAction(item);
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return self.sections[section].footerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.sections[section].headerView;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [((KJTableViewCell *)cell) cellDidDisappear];
}

#pragma mark -- section Management --

- (void)addSection:(KJTableViewSection *)section {
    [self.sections addObject:section];
}

- (void)addSectionsFromArray:(NSArray<KJTableViewSection *> *)array {
    [self.sections addObjectsFromArray:array];
}

- (void)removeSection:(KJTableViewSection *)section {
    [self.sections removeObject:section];
}

- (void)removeAllSections {
    [self.sections removeAllObjects];
}

#pragma mark -- cell Managment --

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
    [self.tableView deselectRowAtIndexPath:indexPath animated:animated];
}

- (void)reloadSectionAtIndex:(NSInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    if (section >= self.sections.count) return;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:animation];
}

#pragma mark -- scroll Delegate --

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.tableView endEditing:YES];
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
            [self.scrollDelegate scrollViewDidScroll:scrollView];
        };
    }
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewDidZoom:)]) {
            [self.scrollDelegate scrollViewDidZoom:scrollView];
        };
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
            [self.scrollDelegate scrollViewWillBeginDragging:scrollView];
        };
    }
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) {
            [self.scrollDelegate scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset];
        };
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)]) {
            [self.scrollDelegate scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
        };
    }
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewWillBeginDecelerating:)]) {
            [self.scrollDelegate scrollViewWillBeginDecelerating:scrollView];
        };
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) {
            [self.scrollDelegate scrollViewDidEndDecelerating:scrollView];
        };
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewDidEndScrollingAnimation:)]) {
            [self.scrollDelegate scrollViewDidEndScrollingAnimation:scrollView];
        };
    }
}

- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(viewForZoomingInScrollView:)]) {
            return [self.scrollDelegate viewForZoomingInScrollView:scrollView];
        };
    }
    return nil;
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewWillBeginZooming:withView:)]) {
            [self.scrollDelegate scrollViewWillBeginZooming:scrollView withView:view];
        };
    }
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewDidEndZooming:withView:atScale:)]) {
            [self.scrollDelegate scrollViewDidEndZooming:scrollView withView:view atScale:scale];
        };
    }
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewShouldScrollToTop:)]) {
            return [self.scrollDelegate scrollViewShouldScrollToTop:scrollView];
        };
    }
    return YES;
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
    if (self.scrollDelegate) {
        if ([self.scrollDelegate respondsToSelector:@selector(scrollViewDidScrollToTop:)]) {
            [self.scrollDelegate scrollViewDidScrollToTop:scrollView];
        };
    }
}

- (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView {
    if (@available(iOS 11.0, *)) {
        if (self.scrollDelegate) {
            if ([self.scrollDelegate respondsToSelector:@selector(scrollViewDidChangeAdjustedContentInset:)]) {
                [self.scrollDelegate scrollViewDidChangeAdjustedContentInset:scrollView];
            };
        }
    }
}

#pragma mark -- lazy Loading --

- (NSMutableArray<KJTableViewSection *> *)sections {
    if (!_sections) {
        _sections = [NSMutableArray array];
    }
    return _sections;
}

@end

